package com.example.hospital.mapper;

import com.example.hospital.domain.entity.Position;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PositionMapper {
    @Insert("INSERT INTO position (position_name) VALUES (#{positionName})")
    @Options(useGeneratedKeys = true, keyProperty = "positionId")
    int insert(Position position);

    @Update("UPDATE position SET position_name=#{positionName} WHERE position_id=#{positionId}")
    int update(Position position);

    @Delete("DELETE FROM position WHERE position_id=#{positionId}")
    int delete(@Param("positionId") Long positionId);

    @Select("SELECT * FROM position WHERE position_id=#{positionId}")
    Position findById(@Param("positionId") Long positionId);

    @Select({
            "<script>",
            "SELECT * FROM position",
            "<where>",
            "  <if test=\"positionName != null and positionName != ''\"> AND position_name LIKE CONCAT('%', #{positionName}, '%') </if>",
            "</where>",
            "ORDER BY position_id DESC",
            "LIMIT #{limit} OFFSET #{offset}",
            "</script>"
    })
    List<Position> list(@Param("positionName") String positionName, @Param("limit") int limit, @Param("offset") int offset);

    @Select({
            "<script>",
            "SELECT COUNT(1) FROM position",
            "<where>",
            "  <if test=\"positionName != null and positionName != ''\"> AND position_name LIKE CONCAT('%', #{positionName}, '%') </if>",
            "</where>",
            "</script>"
    })
    long count(@Param("positionName") String positionName);
}